% to produce the paper results
%%******************************************************************************************
%% Replication code, Figure 4:  Optimal Markov Polices: Two-Country, One-Sector
%% "Technological Rivalry and Optimal Dynamic Policy in an Open Economy"
%% by Bai, Jin, and Lu, May 2025
%%******************************************************************************************

xx1= 2;% period 1 is steady state
xx2 = 100;
ccr = 'b';
lwd = 2;
smc =18;
fposition =[100,100,1200,300];


load Ramsey_sH2.5_sF2.5.mat

ncountries=2;
nsectors =1;
n = ncountries;
ns = nsectors;
inorm = 200; % normalization according to the second point
idiff = 0;%1 plot difference

vname={'T1','T2','taux','x1','x2','w1','w2','P2','pi11','pi22','rmul','MULT_4','MULT_3','r1','r2'};

v1 = {'theta1'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
theta1 = M_.params(ttt);

nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['ri',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    yvar(i,:) = oo_.endo_simul(ivname(i),:);
end

ryvar=yvar;

Tperiod = 1:length(yvar);


%% Markov results
load ./markov2_perfect/output/markov2_perfect_results.mat

ccr2='r';

v1 = {'theta'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
theta = M_.params(ttt);

vname={'T11','T21','taux21','x1','x2','w1','w2','P2','wedge1','pi111','pi221','rr21', 'gammar21', 'dGT2111','rr11'};

nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['mi',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    yvar(i,:) = oo_.endo_simul(ivname(i),:);
end

myvar=yvar;

%% Figure 4(a), Markov export tax
figure(4)
header = 'Figure 4:Optimal Markov Polices: Two-Country, One-Sector';
findex = 4;
F4 = figure(findex);
set(F4,'numbertitle','off')
set(F4,'name',header)

subplot(1,3,1)
hold on
yy = 100*(myvar(mitaux21,2:end)/myvar(mitaux21,inorm)-1);
plot(Tperiod(2:end),yy,'Color',ccr2,'LineWidth',lwd)
y1=myvar(mipi221,2:end);
y2 = (1+theta*y1)./(theta*y1)-1;
yy = 100*(y2/y2(inorm)-1);
plot(Tperiod(2:end),yy,'Color',ccr2,'LineWidth',lwd,'LineStyle','-.')
set(gca,'FontSize',smc)
text(10,-0.5,'\tau_x','FontSize',smc,'Color',ccr2)
text(20,-2,'1/(\theta\pi_{22})','FontSize',smc,'Color',ccr2)
xlim([xx1,60])
ylabel('percent')
ylim([-7,0])
yticks([-7:1:0])
xticks([0:20:60])
title('(a) Export tax')
box on


%% Figure 4(b), Markov innovation subsidy
subplot(1,3,2)

if (idiff==0)
    yy = 100*myvar(miwedge1,2:end);
else
    yy = 100*(myvar(miwedge1,2:end)-myvar(miwedge1,1));
end
plot(Tperiod(2:end),yy,'Color',ccr2,'LineWidth',lwd)
set(gca,'FontSize',smc)
xlim([xx1,60])
ylabel('percent')
ylim([0,0.2])
yticks([0:0.05:0.2])
xticks([0:20:60])

title('(b) Innovation subsidy')
box on



% %% Figure 4(c), Export tax: Markov & Ramsey
subplot(1,3,3)
hold on
yy = 100*(myvar(mitaux21,2:end)/myvar(mitaux21,inorm)-1);
plot(Tperiod(2:end),yy,'Color',ccr2,'LineWidth',lwd)
yy = 100*(ryvar(ritaux,2:end)/ryvar(ritaux,inorm)-1);
plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)

set(gca,'FontSize',smc)
text(2,-0.4,'Markov','FontSize',smc,'Color',ccr2)
text(20,-3.3,'Ramsey','FontSize',smc,'Color',ccr)
xlim([xx1,xx2])
ylabel('percent')
ylim([-7,0])
yticks([-7:1:0])
xticks([0:20:100])
title('(c) Export tax: Markov & Ramsey')
box on

set(F4,'position',fposition)
